﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GraphDrawer {
	public enum WizardAction {
		Exit,
		Previous,
		Next,
		Finish
	}

	public class WizardEventArgs : EventArgs {
		int _iCurrentPageIndex = 0;
		int _iNewPageIndex = 0;
		WizardAction _eAction;

		public WizardEventArgs(int iCurrentPageIndex, int iNewPageIndex, WizardAction eAction) {
			_iCurrentPageIndex = iCurrentPageIndex;
			_iNewPageIndex = iNewPageIndex;
			_eAction = eAction;
			Cancel = false;
		}

		public bool Cancel {
			get;
			set;
		}
		public int CurrentPageIndex {
			get {
				return _iCurrentPageIndex;
			}
		}
		public int NewPageIndex {
			get {
				return _iNewPageIndex;
			}
		}
		public WizardAction Action {
			get {
				return _eAction;
			}
		}
	}

	public class SwitcherControl : UserControl {
		public SwitcherControl() : base() {
			SizeChanged += new SizeChangedEventHandler(SwitcherControl_SizeChanged);
		}
		
		public void SwitchPage(UserControl oPage) {
			//oPage.Background = new SolidColorBrush(Colors.Transparent);
			oPage.Margin = new Thickness(5, 5, 5, 5);
			oPage.Width = this.Width - 10;
			oPage.Height = this.Height - 10;

			Content = oPage;
		}

		void SwitcherControl_SizeChanged(object sender, SizeChangedEventArgs e) {
			if (Content != null) {
				((UserControl)Content).Width = this.Width - 10;
				((UserControl)Content).Height = this.Height - 10;
			}
		}
	}

	public delegate void WizardPageEvent(Wizard sender, WizardEventArgs e);

	public partial class Wizard : UserControl {
		int _iCurrentPage = 0;

		public event WizardPageEvent PageEvent;

		public Wizard() {
			// Initialise page storage
			// -------------------------------
			this.Pages = new List<UserControl>();

			InitializeComponent();

			btExit.Click += OnExitClick;
			btNext.Click += OnNextClick;
			btPrevious.Click += OnPreviousClick;

			Loaded += OnLoaded;
		}


		public static readonly DependencyProperty TitleProperty =
		   DependencyProperty.RegisterAttached("Title", typeof(string), typeof(Wizard), null);

		public string Title {
			get {
				return (string)GetValue(TitleProperty);
			}
			set {
				SetValue(TitleProperty, value);
				txtTitle.Text = (string)value;
			}
		}

		public static readonly DependencyProperty PagesProperty =
			DependencyProperty.RegisterAttached("Pages", typeof(List<UserControl>), typeof(Wizard), null);

		public List<UserControl> Pages {
			get {
				return (List<UserControl>)GetValue(PagesProperty);
			}
			set {
				SetValue(PagesProperty, value);
			}
		}

		void OnLoaded(object sender, RoutedEventArgs e) {
			UpdateView();
		}

		void OnExitClick(object sender, RoutedEventArgs e) {
			FirePageEvent(_iCurrentPage, _iCurrentPage, WizardAction.Exit);
			_iCurrentPage = 0;
		}

		void OnNextClick(object sender, RoutedEventArgs e) {
			if (_iCurrentPage == Pages.Count - 1) {
				FirePageEvent(_iCurrentPage, -1, WizardAction.Finish);
				_iCurrentPage = 0;
			}
			else {
				if (FirePageEvent(_iCurrentPage, _iCurrentPage + 1, WizardAction.Next)) {
					_iCurrentPage++;

					UpdateView();
				}
			}
		}

		void OnPreviousClick(object sender, RoutedEventArgs e) {
			if (FirePageEvent(_iCurrentPage, _iCurrentPage - 1, WizardAction.Previous)) {
				_iCurrentPage--;

				UpdateView();
			}
		}

		bool FirePageEvent(int iCurrentPageIndex, int iNewPageIndex, WizardAction eAction) {
			WizardEventArgs oArgs =
					new WizardEventArgs(iCurrentPageIndex, iNewPageIndex, eAction);

			if (PageEvent != null)
				PageEvent(this, oArgs);

			return !oArgs.Cancel;
		}

		void UpdateView() {
			ctrPageHost.SwitchPage(Pages[_iCurrentPage]);

			if (_iCurrentPage == 0)
				btPrevious.IsEnabled = false;
			else if (_iCurrentPage == Pages.Count - 1)
				btNext.Content = "Finish";
			else {
				btPrevious.IsEnabled = true;
				btNext.Content = "Next";
			}
		}
	}
}
